Imports System.Data
Imports System.Data.SqlClient

Public Class SMSReminder

    Private Sub SMSReminder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadIcon(Me)
        RefreshGrid()

        Dim str As String = "SELECT * FROM SMSMessage WHERE ID  = 1"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader
        While dr.Read
            Me.txtSMS.Text = dr.Item("str").ToString
            Me.chkActive.EditValue = dr.Item("Active")
        End While
        dr.Close()
    End Sub

    Private Sub RefreshGrid()

        Dim str As String
        str = "SELECT     dbo.People.*, dbo.Category_People.CategoryName AS MemberLevel " & _
        "FROM         dbo.People INNER JOIN " & _
        "dbo.Category_People ON dbo.People.Category = dbo.Category_People.CategoryID " & _
        "WHERE Active = 1"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds, "People")
        Me.GridControl1.DataSource = ds.Tables(0)

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim str As String
        str = "UPDATE People SET BirthdaySMS = @BirthdaySMS WHERE ID = @ID"
        Dim cmd As New SqlCommand(str, gCnnPCE)

        Dim i As Integer
        Dim intRowCount As Integer
        intRowCount = GridView1.RowCount
        Dim _Select As Integer

        For i = 0 To intRowCount - 1
            _Select = GridView1.GetRowCellValue(i, GridView1.Columns("BirthdaySMS"))
            cmd.Parameters.Clear()
            'cmd.Parameters.Add("@BirthdaySMS", SqlDbType.Int).Value = GridView1.GetRowCellValue(i, GridView1.Columns("Select"))
            'cmd.Parameters.Add("@ID", SqlDbType.Int).Value = GridView1.GetRowCellValue(i, GridView1.Columns("ID"))
            cmd.Parameters.Add("@BirthdaySMS", SqlDbType.Int).Value = _Select
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = GridView1.GetRowCellValue(i, GridView1.Columns("ID"))
            cmd.ExecuteNonQuery()
        Next

        str = "UPDATE SMSMessage SET str = @str, Active = @Active WHERE ID = 1"
        cmd.CommandText = str
        cmd.Parameters.Clear()
        cmd.Parameters.Add("@str", SqlDbType.NVarChar).Value = Me.txtSMS.Text
        cmd.Parameters.Add("@Active", SqlDbType.Int).Value = Me.chkActive.EditValue
        cmd.ExecuteNonQuery()

        MsgBox("Save Successfully")
        Me.Close()

    End Sub

    Private Sub chkActive_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkActive.CheckedChanged
        'check sms account

        If chkActive.Checked = True Then
            Dim str As String = "SELECT UserID FROM SMSAccountSetting WHERE ID = 1"
            Dim cmd As New SqlCommand(str, gCnnPCE)
            If cmd.ExecuteScalar.ToString = "" Then
                MsgBox("Please Set Your SMS Account Before Enable This Function")
                chkActive.Checked = False
                Exit Sub
            End If
        End If


    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub lnkSMSAccountSetting_OpenLink(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.OpenLinkEventArgs) Handles lnkSMSAccountSetting.OpenLink
        Dim frm As New SMSSetting
        frm.ShowDialog()

    End Sub
End Class